#include <iostream>
#include <ostream>
using namespace std;
class Complex
{
public:
    // 1. 带默认参数值的构造函数
    Complex(double r = 0.0, double i = 0.0) : _real(r), _imag(i) {}
    // 2. 拷贝构造函数
    Complex(const Complex &other)
    {
        _real = other._real;
        _imag = other._imag;
    }
    // 3. 支持两个复数加法运算
    Complex operator+(const Complex &other) const
    {
        return Complex(_real + other._real, _imag + other._imag);
    }
    // 4. 支持两个复数相减运算
    Complex operator-(const Complex &other) const
    {
        return Complex(_real - other._real, _imag - other._imag);
    }
    // 5. 支持“cout << a”其中a为复数
    friend ostream &operator<<(ostream &out, const Complex &c)
    {
        if (c._real != 0)
            out << c._real;
        if (c._imag < 0)
            out << " - " << -c._imag << "i";
        else if (c._imag > 0)
            out << " + " << c._imag << "i";
        return out;
    }
    // 6. 支持赋值运算
    Complex &operator=(const Complex &other)
    {
        if (this != &other) // 防止自赋值
        {
            _real = other._real;
            _imag = other._imag;
        }
        return *this;
    }

private:
    double _real; // 实部
    double _imag; // 虚部
};

int main()
{
    Complex a(3, 4);   // 构造函数
    Complex b(a);      // 拷贝构造
    Complex c;         // 默认构造
    c = a + b;         // 加法运算
    Complex d = c - a; // 减法运算

    cout << "c = " << c << endl;
    cout << "d = " << d << endl;

    return 0;
}